package com.chushouya.controller.product.admin;

import com.chushouya.common.constant.ServiceType;
import com.chushouya.product.rabbitmq.sender.SuhuishouSyncMessageSender;
import com.chushouya.product.service.sync.SuhuishouSyncService;
import com.general.framework.core.schema.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 速回收同步控制器
 * 
 * @author system
 */
@Slf4j
@RestController
@RequestMapping(ServiceType.PLATFORM + "/sync/suhuishou")
@Api(tags = ServiceType.PLATFORM + "速回收同步管理")
public class SuhuishouSyncController {

    @Resource(name = "productSuhuishouSyncService")
    private SuhuishouSyncService suhuishouSyncService;
    
    @Resource
    private SuhuishouSyncMessageSender suhuishouSyncMessageSender;

    @PostMapping("/brand/all")
    @ApiOperation("同步所有分类的品牌")
    public Response<Void> syncAllBrands() {
        try {
            suhuishouSyncService.syncBrandList();
            return Response.success();
        } catch (Exception e) {
            log.error("同步所有品牌失败", e);
            return Response.error("同步失败：" + e.getMessage());
        }
    }

    @PostMapping("/brand")
    @ApiOperation("同步指定分类的品牌")
    public Response<Void> syncBrandByCategory(
            @ApiParam(value = "分类ID", required = true) @RequestParam Long categoryId) {
        try {
            suhuishouSyncService.syncBrand(categoryId);
            return Response.success();
        } catch (Exception e) {
            log.error("同步分类{}的品牌失败", categoryId, e);
            return Response.error("同步失败：" + e.getMessage());
        }
    }

    @PostMapping("/product/all")
    @ApiOperation("同步所有品牌的产品")
    public Response<Void> syncAllProducts() {
        try {
            suhuishouSyncService.syncProductList();
            return Response.success();
        } catch (Exception e) {
            log.error("同步所有产品失败", e);
            return Response.error("同步失败：" + e.getMessage());
        }
    }

    @PostMapping("/product")
    @ApiOperation("同步指定分类和品牌的产品")
    public Response<Void> syncProductByCategoryAndBrand(
            @ApiParam(value = "分类ID", required = true) @RequestParam Long categoryId,
            @ApiParam(value = "品牌ID", required = true) @RequestParam Long brandId) {
        try {
            suhuishouSyncService.syncProduct(categoryId, brandId);
            return Response.success();
        } catch (Exception e) {
            log.error("同步分类{}品牌{}的产品失败", categoryId, brandId, e);
            return Response.error("同步失败：" + e.getMessage());
        }
    }

    @PostMapping("/brand/async")
    @ApiOperation("异步同步指定分类的品牌")
    public Response<Void> asyncSyncBrandByCategory(
            @ApiParam(value = "分类ID", required = true) @RequestParam Long categoryId) {
        try {
            suhuishouSyncMessageSender.sendBrandSyncMessage(categoryId);
            return Response.success();
        } catch (Exception e) {
            log.error("发送分类{}的品牌同步消息失败", categoryId, e);
            return Response.error("发送消息失败：" + e.getMessage());
        }
    }

    @PostMapping("/product/async")
    @ApiOperation("异步同步指定分类和品牌的产品")
    public Response<Void> asyncSyncProductByCategoryAndBrand(
            @ApiParam(value = "分类ID", required = true) @RequestParam Long categoryId,
            @ApiParam(value = "品牌ID", required = true) @RequestParam Long brandId) {
        try {
            suhuishouSyncMessageSender.sendProductSyncMessage(categoryId, brandId);
            return Response.success();
        } catch (Exception e) {
            log.error("发送分类{}品牌{}的产品同步消息失败", categoryId, brandId, e);
            return Response.error("发送消息失败：" + e.getMessage());
        }
    }

    @PostMapping("/brand/async/all")
    @ApiOperation("异步同步所有分类的品牌")
    public Response<Void> asyncSyncAllBrands() {
        try {
            suhuishouSyncService.syncBrandList();
            return Response.success();
        } catch (Exception e) {
            log.error("批量发送品牌同步消息失败", e);
            return Response.error("发送消息失败：" + e.getMessage());
        }
    }

    @PostMapping("/product/async/all")
    @ApiOperation("异步同步所有品牌的产品")
    public Response<Void> asyncSyncAllProducts() {
        try {
            suhuishouSyncService.syncProductList();
            return Response.success();
        } catch (Exception e) {
            log.error("批量发送产品同步消息失败", e);
            return Response.error("发送消息失败：" + e.getMessage());
        }
    }
}
